package com.example.demo.controller;


import com.example.demo.Service.IOrderService;
import com.example.demo.Service.impl.IOrderServiceImpl;
import com.example.demo.entity.params.OrderCreateParam;
import com.example.demo.entity.vo.OrderSelectVo;
import com.example.demo.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
@CrossOrigin(origins = "*")
public class OrderController {

    @Autowired
    private IOrderService iOrderService;

    @PostMapping("/create")
    public Map<String,Object> createOrder( OrderCreateParam orderCreateParam,HttpServletRequest request){

        Long userId = JWTUtil.getUserId(request.getHeader("Authorization"));

        int rs = iOrderService.createOrders(orderCreateParam.getShoppingCartIds(),userId);

        Map<String,Object> map = new HashMap<>();

        if (rs>0){

            map.put("code",200);
            map.put("msg","操作成功");

        }
        else {
            map.put("code",500);
            map.put("msg","操作失败");
        }
        return map;

    }

    @GetMapping("/select")
    public Map<String,Object> selectOrder(HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        List<OrderSelectVo> list = iOrderService.selectOrders(JWTUtil.getUserId(request.getHeader("Authorization")));

        map.put("data" ,list);
        map.put("code",200);
        map.put("msg","查询成功");

        return map;

    }
}
